package org.zfes.snowier.cloud.zbss.widget.act.service.impl;

import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZObjectUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.widget.act.service.IActQuestionCategoryService;
import org.zfes.snowier.cloud.zbss.widget.act.dao.ActQuestionCategoryMapper;
import org.zfes.snowier.cloud.zbss.widget.act.model.ActQuestionCategory;
import java.util.stream.Stream;
@Service
public class ActQuestionCategoryServiceImpl implements IActQuestionCategoryService{
 	@Autowired
	private ActQuestionCategoryMapper actQuestionCategoryMapper;
	
 	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createActQuestionCategory(ActQuestionCategory actQuestionCategory) {
		ZBeanUtil.setCreateInfo(actQuestionCategory);
		ZBeanUtil.validateBean(actQuestionCategory);
		actQuestionCategoryMapper.insertSelective(actQuestionCategory);
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<ActQuestionCategory> loadActQuestionCategoryById(Long id) {
		if(id!=null){
			return Optional.ofNullable(actQuestionCategoryMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateActQuestionCategory(ActQuestionCategory actQuestionCategory) {
		ZBeanUtil.setUpdatedInfo(actQuestionCategory);
		ZBeanUtil.validateBean(actQuestionCategory);
		actQuestionCategoryMapper.updateByPrimaryKeySelective(actQuestionCategory);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteActQuestionCategory(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		actQuestionCategoryMapper.deleteByPrimaryKey(id);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setActQuestionCategoryStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		actQuestionCategoryMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadActQuestionCategoryDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		
		pm.getStr("pm").ifPresent(val->pm.updateParam("pm", "%"+val+"%"));
		
		DataSet ds=DataSet.newDS2(actQuestionCategoryMapper.selectListCount(pm), actQuestionCategoryMapper.selectMapListPage(pm));
		return ds;
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void addQuestionCount(Long id, int countToAdd) {
		if(ZObjectUtil.isNotNull(id)) {
			actQuestionCategoryMapper.updateAddQuestionCount(id,countToAdd);
		}
		
	}
	
}